#include "tinyxml2.h"
#include<iostream>
#include <cerrno>
#include<fstream>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include<string>
#include<vector>
#include<regex>

#if defined( _MSC_VER ) || defined (WIN32)
#include <crtdbg.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
_CrtMemState startMemState;
_CrtMemState endMemState;
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif

using namespace tinyxml2;
using namespace std;

int gPass = 0;
int gFail = 0;

struct Xml
{
public:
    string _title;
    string _link;
    string _description;
    string _content;
};
class Rss
{
public:
    Rss(size_t size)
    {
        _rss.reserve(size);
    }
    void read(const string & filename)
    {

        Xml xml;
        XMLDocument doc ;
        doc.LoadFile(filename.data());
        if(doc.ErrorID())
        {
            cout<<"打开失败"<<endl;
            return ;
        }
        XMLElement* itemNode=doc.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("item");
        while(itemNode)
        {

            xml._title=itemNode->FirstChildElement("title")->GetText();
            xml._link=itemNode->FirstChildElement("link")->GetText();
            string description=itemNode->FirstChildElement("description")->GetText();
            string  content=itemNode->FirstChildElement("content:encoded")->GetText();

            regex reg("<[^>]*>");
            description=regex_replace(description,reg,"");
            content=regex_replace(content,reg,"");

            xml._description=description;
            xml._content=content;

            _rss.push_back(xml);

            itemNode->NextSiblingElement("item"); 
        }
    }
    void write(string & filename)
    {
        ofstream ofs(filename);
        if(!ofs)
        {
            cerr<<"open "<<filename<<" faile"<<endl;
            return ;
        }
        for(auto&i:_rss)
        {
            ofs<<"some"<<i._title<<endl;
        }
        ofs.close();
    }
private:
    vector<Xml> _rss;
};


